import json
import os.path
import sys
import unittest

import requests


class MyTestCase(unittest.TestCase):
    def test_server_pressed(self):
        file_path = os.path.sep.join([sys.path[0], '..', '..', 'server.json'])
        print(file_path)
        with open(file_path, 'r') as file:
            conf = json.loads(file.read())
        host = '127.0.0.1'
        port = conf['listen_port']

        attr = 10
        error = 0
        url = f'http://{host}:{port}/send'
        params = {
            "content": "内容",
            "title": "标题",
            "token": "凭证"
        }
        for i in range(attr):
            try:
                requests.get(url=url, params=params, timeout=1).json()
            except Exception:
                error += 1
        print(f"失败/成功: {error}/{attr}")
        self.assertTrue(error == 0)  # add assertion here


if __name__ == '__main__':
    unittest.main()
